package com.ttbj.controller.zwrefund;

import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.zwrefund.ZwRefundDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.zwrefund.ZwPayTypeEnum;
import com.ttbj.api.zwrefund.ZwRefundCodeEnum;
import com.ttbj.api.zwrefund.ZwRefundService;
import com.ttbj.constant.annotation.SystemControllerLog;
import com.ttbj.core.util.IpUtil;
import com.ttbj.util.ViewDataUtil;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ZwRefundController {
	
	@Autowired
	private ZwRefundService zwRefundService;
	
	/**
	 * 退款申请单列表页面
	 * @return
	 */
	@RequestMapping(value="/zwrefund/zwrefund_info.htm")
	public String zwrefund_info() {
		return "zwrefund/zwrefund_info";
	}
	
	/**
	 * 查询所有未审核的退款申请单列表（包括未审核的，退款异常的，退款关闭的）
	 * @param refundOrderNum 订单编号
	 * @param refundPayNum 支付时的支付编号
	 * @param page 分页信息
	 * @return
	 */
	@RequestMapping(value="/zwrefund/getUnauditedZwRefundInfoByPage.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getUnauditedZwRefundInfoByPage(HttpServletRequest request,String refundOrderNum, String refundPayNum, Page page) {
		System.out.println("解析的IP地址是：" + IpUtil.getIpAddr(request));
		//只获取未审核的，退款异常的，退款关闭的退款申请单
		Integer[] refundCode = {ZwRefundCodeEnum.Unaudited.getCode(),
									ZwRefundCodeEnum.Abnormal.getCode(),ZwRefundCodeEnum.Closed.getCode()};
		List<ZwRefundDto> zwRefundDtos = this.zwRefundService.findZwRefundInfoByPage(refundOrderNum,refundPayNum, refundCode, page);
		int count = 0;
		if (null != zwRefundDtos) {
			count = this.zwRefundService.findZwRefundInfoByPageCount(refundOrderNum,refundPayNum, refundCode);
		}
		return ViewDataUtil.buildSuccessPageJson("退款申请", zwRefundDtos, count);
	}
	
	/**
	 * 查询所有已审核的退款申请单列表（包括未通过审核的，审核通过的，退款处理中的）
	 * @param refundOrderNum 订单编号
	 * @param refundPayNum 支付时的支付编号
	 * @param page 分页信息
	 * @return
	 */
	@RequestMapping(value="/zwrefund/getAuditZwRefundInfoByPage.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getAuditZwRefundInfoByPage(String refundOrderNum, String refundPayNum, Page page) {
		//只获取未通过审核的，审核通过的，退款处理中的退款申请单
		Integer[] refundCode = {ZwRefundCodeEnum.Audit_Failed.getCode(), 
									ZwRefundCodeEnum.Audit_Pass.getCode(), ZwRefundCodeEnum.In_Hand.getCode()};
		List<ZwRefundDto> zwRefundDtos = this.zwRefundService.findZwRefundInfoByPage(refundOrderNum,refundPayNum, refundCode, page);
		int count = 0;
		if (null != zwRefundDtos) {
			count = this.zwRefundService.findZwRefundInfoByPageCount(refundOrderNum,refundPayNum, refundCode);
		}
		return ViewDataUtil.buildSuccessPageJson("退款申请", zwRefundDtos, count);
	}
	
	/**
	 * 微信、支付宝退款操作
	 * @param refundType 退款方式：0微信，1支付宝，
	 * @param refundAmt 退款金额：单位元
	 * @param refundPayNum 支付时使用的支付编号
	 * @param refundId 退款申请列表中的主键id
	 * @param refundOrder 订单id （退款后更新订单表，分配业务员，分配司机中的删除状态）
	 * @return
	 */
	@RequestMapping(value="/zwrefund/modifyZwRefundOperation.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="微信、支付宝退款")
	public String modifyZwRefundOperation(@RequestParam Integer refundType, 
										  @RequestParam String refundAmt, 
										  @RequestParam String refundPayNum, 
										  @RequestParam Integer refundId,
										  @RequestParam Long refundOrder) {
		try {
			//退款
			this.zwRefundService.zwRefundOperation(refundType, refundAmt, refundPayNum, refundId, refundOrder);
			return ViewDataUtil.buildSuccessJson("操作成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 支付宝和微信退款查询
	 * @param refundType 支付方式 0微信，1支付宝
	 * @param refundPayNum 支付时的支付编号
	 * @param refundCode 审核状态
	 * @param outRequestNo 支付宝用到的退款请求号
	 * @param refundId 退款单的主键id
	 * @return
	 */
	@RequestMapping(value="/zwrefund/zwRefundQuery.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String zwRefundQuery(@RequestParam Integer refundType, @RequestParam String refundPayNum, @RequestParam Integer refundCode,
								@RequestParam(required=false) String outRequestNo, @RequestParam Integer refundId) {
		try {
			if (refundType == ZwPayTypeEnum.WX.getCode()) {	//如果是微信退款查询
				int code = this.zwRefundService.wxRefundQuery(refundPayNum, refundCode, refundId);
				return ViewDataUtil.buildSuccessJson("退款查询成功", code);
			}else if (refundType == ZwPayTypeEnum.ZFB.getCode()) {	//如果是支付宝退款查询
				int code = this.zwRefundService.zfbRefundQuery(refundPayNum, outRequestNo, refundCode, refundId);
				return ViewDataUtil.buildSuccessJson("退款查询成功", code);
			}else {
				return ViewDataUtil.buildFailureJson(1, "查询有误");
			}
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 设置审核状态为未通过审核
	 * @param refundCode 未通过审核状态：2
	 * @param refundOrder 退款单的id
	 * @param orderId 订单id （退款后更新订单表，分配业务员，分配司机中的删除状态）
	 * @return
	 */
	@RequestMapping(value="/zwrefund/modifyRefundAuditFailed.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="退款设置为未通过")
	public String modifyRefundAuditFailed(@RequestParam Integer refundId, @RequestParam Long refundOrder) {
		try {
			this.zwRefundService.editRefundAuditFailed(ZwRefundCodeEnum.Audit_Failed.getCode(), refundId, refundOrder);
			return ViewDataUtil.buildSuccessJson("更改成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
}
